/**
 * Created by zhaocong on 2016-7-29 09:54:03.
 * 房掌柜
 *
 * @extends timepicker 时间插件
 * @extends tker-css
 */
define(['timepicker', 'tker-css', 'calendar'], function (require, exports, module) {
  "use strict";
  var cc = window.cc || {};

  /**
   * 新增房型
   */
  cc.addHouse = function () {

    // 时间插件
    $('.form_datetime').datetimepicker({
      language: 'zh-CN',
      weekStart: 1,
      todayBtn: 1,
      autoclose: 1,
      todayHighlight: 1,
      startView: 2,
      forceParse: 0,
      showMeridian: 1,
      minView: 2,
      format: "yyyy/mm/dd"
    });

    // 选择的时间
    var selday = function (tag) {
      var seld = [];
      $('.' + tag).calendar({
        width: 280,
        onSelected: function (view, date, data) {
          var year = date.getFullYear(),
              MM = date.getMonth() + 1,
              dat = date.getDate(),
              MM = MM < 10 ? '0' + MM : MM,
              dat = dat < 10 ? '0' + dat : dat,
              d = year + '-' + MM + '-' + dat;
          if (seld.indexOf(d) < 0) {
            seld.push(d);
            $('#' + tag).val(seld);
            $('.invent-datelist[data-id="' + tag + '"]').append('<li class="list-group-item">' + d + '<i class="fa fa-close seld-x"></i></li>');
          }
        }
      });
      // 移除选择的时间
      $('.invent-datelist').on('click', '.seld-x', function () {
        var par = $(this).parent();
        seld.splice(seld.indexOf(par.text()), 1);
        $('#' + tag).val(seld);
        par.remove();
      });
    };
    (function (selday) {
      var i = 1;
      while (i < 7) {
        selday('ci' + i);
        i++;
      }
    })(selday);

    var app = angular.module('app', []);
    app.controller('appCtrl', function ($scope, $http) {

      // 服务器数据
      $scope.data = window.data;

      // 格式日历
      var f = function (a, g) {
        var g = g || 'y/m/d',
            date = new Date(a),
            y = date.getFullYear(),
            m = date.getMonth() + 1,
            d = date.getDate(),
            m = m < 10 ? '0' + m : m,
            d = d < 10 ? '0' + d : d;
        return g.replace(/y/, y).replace(/m/, m).replace(/d/, d);
      };

      // 生成日期表
      var datelist = function (date) {
        date = new Date(date);
        $scope.year = date.getFullYear();
        $scope.month = date.getMonth() + 1;
        $scope.datelist = (function () {
          var dat = date.getDate(),
              weekName = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
              today = f(new Date()),
              maxd = new Date($scope.year, $scope.month, 0).getDate(),
              next = new Date($scope.year, $scope.month + 1, 0),
              all = [];

          for (var i = 0; i < 7; i++) {
            var day = {};
            if (dat <= maxd) {
              day.i = dat;
              day.id = f(date.setDate(dat));
            } else {
              day.i = dat - maxd;
              day.id = f(next.setDate(day.i));
              day.dis = true;
            }
            day.today = day.id == today;
            day.week = weekName[new Date(day.id).getDay()];
            // 合并服务器数据
            $.extend(day, $scope.data.days[day.id]);
            all[i] = day;
            dat++;
          }
          return all;
        })();

        // 生成房间型号
        $scope.fangx = $scope.data.fangx;

      };
      // 默认加载
      datelist($scope.data.date);

      // 表格操作
      $scope.adh = {
        // 上周
        prev: function () {
          $scope.data.date = new Date($scope.data.date).setDate(new Date($scope.data.date).getDate() - 7);
          this.sub();
        },
        // 今天
        today: function () {
          $scope.data.date = new Date();
          this.sub();
        },
        // 下周
        next: function () {
          $scope.data.date = new Date($scope.data.date).setDate(new Date($scope.data.date).getDate() + 7);
          this.sub();
        },
        // 选择时间
        days: function () {
          var v = $(this).val();
          $scope.$apply(function () {
            $scope.data.date = new Date(v);
            $scope.adh.sub();
          });
        },
        // TODO 添加AJAX 根据选择日期和酒店返回 7日 的数据，并重新赋值到全局变量
        sub:function () {
          $http.post('../../data/fzg.php', {date: f($scope.data.date), jd: $scope.data.jd}).success(function (data) {
            console.log(data);
            // $scope.data = data;
            datelist($scope.data.date);
          });
        }
      };

      // 选择时间 查询数据并重新赋值
      $('#adhdays').on('change', $scope.adh.days);

      // 选择酒店 查询数据并重新赋值
      $('#jdTable').on('click', 'td', function () {
        var _this = $(this);
        _this.parents('#jdTable').find('td').removeClass('active');
        _this.addClass('active');
        if(_this.data('id')){
          $scope.$apply(function () {
            $scope.data.date = new Date();
            $scope.data.jd = _this.data('id');
            $scope.adh.sub();
          });
        }
      });

      // 添加房间
      $scope.roomlist = [{}];
      $scope.roomlistAdd = function () {
        $scope.roomlist.push({});
      };


    });
    angular.bootstrap(document.body, ['app']);
  };

  module.exports = cc;
});